package com.jvtd.eyl.utils;

import android.content.Context;
import android.databinding.BindingAdapter;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jvtd.eyl.R;
//import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.utils.UiUtils;
import com.jvtd.utils.glide.GlideRequestOptionsUtils;

public class DataBindingUtils {
    @BindingAdapter(value = {"imageUrl", "placeholder"})
    public static void loadImage(ImageView imgView, String imageUrl, Drawable placeholder)
    {
        Glide.with(imgView.getContext()).load(imageUrl).apply(GlideRequestOptionsUtils.getOptions
                (placeholder)).into(imgView);
    }

    @BindingAdapter(value = {"isAdd"})
    public static void loadAdd(TextView textView, String isAdd){
        if (TextUtils.equals(isAdd, BookBean.YES)) {
            textView.setEnabled(false);
            textView.setText(R.string.has_add_title);
        }else {
            textView.setEnabled(true);
            textView.setText(R.string.add_book_title);
        }
    }

    @BindingAdapter(value = {"isAdd","selected"})
    public static void loadAdd(CheckBox checkBox, String isAdd, boolean selected){
        if (TextUtils.equals(isAdd, BookBean.NO) && selected) {
            checkBox.setVisibility(View.VISIBLE);
        }else {
            checkBox.setVisibility(View.GONE);
        }
    }

    @BindingAdapter(value = {"bookIndex"})
    public static void loadBookIndex(LinearLayout linearLayout, int index){
        int oneLine = 3;
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) linearLayout.getLayoutParams();
        Context context = linearLayout.getContext();
        if (index % oneLine == 0)
        {
            layoutParams.leftMargin = UiUtils.dp2px(context,20);
            layoutParams.rightMargin = UiUtils.dp2px(context,10);
        }else if (index % oneLine == oneLine - 1){
            layoutParams.leftMargin = UiUtils.dp2px(context,10);
            layoutParams.rightMargin = UiUtils.dp2px(context,20);
        }else {
            layoutParams.leftMargin = UiUtils.dp2px(context,10);
            layoutParams.rightMargin = UiUtils.dp2px(context,10);
        }
        linearLayout.setLayoutParams(layoutParams);
    }

    @BindingAdapter(value = {"myBookIndex"})
    public static void loadMyBookIndex(LinearLayout linearLayout, int myBookIndex){
        Context context = linearLayout.getContext();
        int oneLine = 3;
        int left;
        int right;
        int top = UiUtils.dp2px(context,42);

        if (myBookIndex % oneLine == 0)
        {
            left = UiUtils.dp2px(context,20);
            right = UiUtils.dp2px(context,12);
        }else if (myBookIndex % oneLine == oneLine - 1){
            left = UiUtils.dp2px(context,12);
            right = UiUtils.dp2px(context,20);
        }else {
            left = UiUtils.dp2px(context,12);
            right = UiUtils.dp2px(context,12);
        }
        linearLayout.setPadding(left,top,right,0);
    }
}
